﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using XinYue.Permission.BLL;
using XinYue.Permission.Mode;
using XinYue.Permission.Web.Models;

namespace XinYue.Permission.Web.Controllers
{
    public class MenuController : Controller
    {
        MenuService Menu = new MenuService();

        #region  显示信息
        // GET: Menu
        public JsonResult GetModulePageList(int page, int limit, string name)
        {

            PageListResult<Menu> pageListResult = new PageListResult<Menu>();
            pageListResult.code = 0;
            pageListResult.msg = string.Empty;
            int count = 0;
            Expression<Func<Menu, bool>> Wherelambda = c => true;
            if (name != null)
            {
                Wherelambda = c => c.Name.Contains(name);
            }
            Expression<Func<Menu, int>> OrderBylambda = c => c.Id;
            pageListResult.data = Menu.QueryPageList(page, limit, Wherelambda, OrderBylambda, out count);
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加修改
        public JsonResult AddingInformation(Menu menu)
        {
            Operate operate = new Operate();
            if (menu.Id != 0)
            {
                operate.Success = Menu.Update(menu);
            }
            else
            {
                menu.CreateTime = DateTime.Now;
                operate.Success = Menu.Add(menu);
            }
            return Json(operate);
        }
        #endregion

        #region 判断是否存在
        public JsonResult SelectMenuName(string name)
        {
            Operate operate = new Operate();
            Expression<Func<Menu, bool>> lambdaExpression = a => a.Name == name;
            operate.Success = Menu.Exists(lambdaExpression);
            return Json(operate);
        }
        #endregion

        #region 查询一个数据
        public JsonResult SelectMenu(int Id)
        {
            Menu menu = new Menu();
            Expression<Func<Menu, bool>> lambdaExpression = a => a.Id == Id;
            menu = Menu.Query(lambdaExpression);
            return Json(menu);
        }
        #endregion

        #region 修改状态
        public JsonResult GetUpdateMenuIsShow(Menu menu)
        {
            Operate operate = new Operate();
            operate.Success = Menu.Update(menu);
            return Json(operate);
        }
        #endregion

        #region 删除
        public JsonResult DelMenuId(Menu menu)
        {
            Operate operate = new Operate();
            operate.Success = Menu.Delete(menu);
            return Json(operate);
        }

        #endregion
    }
}